package ai;

import engine.data.City;
import engine.data.Unit;

public abstract class ProductionTask {

	public short type;

	public String name;

	public String details;

	public byte status;

	public short priority;

	public Localizable target;

	public City assignedCity;

	public ProductionTask() {
	}

	public ProductionTask(short type, String name, String details,
			short priority, Localizable target) {
		this.type = type;
		this.name = name;
		this.details = details;
		this.status = AIConstant.STATUS_WAIT_4_CITY;
		this.priority = priority;
		this.target = target;
	}

	public void assign(City taskDoer) {
		if (taskDoer != null)
			this.assignedCity = taskDoer;
	}

	public abstract boolean isTaskSuitable(City city);

	public abstract short getFormula(City taskDoer);

	public abstract boolean isProductionAchieved();

	public void checkStatus() {
		if (this.assignedCity == null)
			this.status = AIConstant.STATUS_WAIT_4_CITY;
		if (isProductionAchieved())
			this.status = AIConstant.STATUS_DONE;
	}

	public String getTargetToString() {
		if (target != null) {
			if (target instanceof Unit)
				return ((Unit) this.target).toStringShort();
			return target.toString();
		}
		return "";
	}

	public String toString() {
		// TODO Auto-generated method stub
		StringBuffer res = new StringBuffer("--> ProductionTask ").append(": ")
				.append(this.name);
		res.append("\n\tStatus\t").append(
				TaskFactory.getStatusName(this.status));
		res.append("\n\tPriority\t").append(this.priority);
		res.append("\n\tTarget\t").append(getTargetToString());
		res.append("\n\tAssignedCity\t").append(this.assignedCity).append("\n");
		return res.toString();
	}

	public boolean equals(ProductionTask obj) {
		// TODO Auto-generated method stub
		return (obj.type == this.type && obj.target == this.target);
	}

}
